<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.Date"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>IPL 2012</title>
</head>
<body>
<%@ include file="global.jsp" %>
<%
Statement st = null; 
ResultSet rs = null; 
Connection con = null; 
PreparedStatement pstmt = null; 
String userName = (String) session.getAttribute("userName");
String league = (String) session.getAttribute("league");
String matchId = (String)session.getAttribute("matchId");
if(matchId!=null){
int SNo = 0;
int id = 0;
try{
	Class.forName(db_driver);
	con = DriverManager.getConnection(db_url, db_user, db_password);
		if(userName==null || ("").equals(userName)){
			System.out.println("userName "+userName);
			session.setAttribute("resposeText","userName Not Entered");
		}else {
			System.out.println("userName "+userName);
			if (userName!=null) {
		      st=con.createStatement();
			  String str = "select id from users where userName='"+userName+"'";
			  rs = st.executeQuery(str);
			  while(rs.next()){
				  id = rs.getInt(1);
				  System.out.println("id "+id);
				  
			  }
		    }
			 if(id > 0){
				 System.out.println("userExist "+userName);
				 if(request.getParameter("winner") !=null && !("--").equals(request.getParameter("winner"))){
				     st=con.createStatement();
					 String str = "select count(*) from answers where userName='"+userName+"' and MatchID = '"+matchId+"'";
					 rs=st.executeQuery(str);
					 while(rs.next()){
						 SNo = rs.getInt(1);
						 if(SNo>0){
						 	session.setAttribute("resposeText","Entry already made");
						 }
					 }
					 if(SNo==0){
						 DateFormat formater = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
						 java.util.Date sysDate = new Date();
						 String dateFormat = formater.format(sysDate);
						 String query = "insert into answers(matchId, winner, mom, player1, player2, sixesWinner, sixes, userName, punch, league, time)values(?,?,?,?,?,?,?,?,?,?,?)";
						 pstmt = con.prepareStatement(query); 
						 pstmt.setString(1, matchId);
						 pstmt.setString(2, request.getParameter("winner"));
						 pstmt.setString(3, request.getParameter("mom"));
						 pstmt.setString(4, request.getParameter("player1"));
						 pstmt.setString(5, request.getParameter("player2"));
						 pstmt.setString(6, request.getParameter("sixTeam"));
						 pstmt.setString(7, request.getParameter("sixes"));
						 pstmt.setString(8, userName);
						 pstmt.setString(9, request.getParameter("punch"));
						 pstmt.setString(10, league);
						 pstmt.setString(11, dateFormat);
						 pstmt.executeUpdate();
						 pstmt.close();
						 session.setAttribute("resposeText","Succesfully Submited");
					 }
				  }
			 }else{
				 session.setAttribute("resposeText","Unauthorized User");
			 }
		 
		 rs.close();
		 st.close();
	 }
}catch(Exception e){
	e.printStackTrace();
	session.setAttribute("resposeText",e.getMessage());
}finally{
	
}
}
response.sendRedirect("matchSelect.jsp");
%>

</body>
</html>